//go:build failover || all
// +build failover all

package test

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"fmt"
	"k8gbterratest/utils"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestFullFailover(t *testing.T) {
	for _, ingressType := range utils.IngressTypes {
		const basePath = "../examples/failover"
		const host = "terratest-failover.cloud.example.com"

		workflowEU := utils.NewWorkflow(t, "k3d-test-gslb1", 5053)
		workflowUS := utils.NewWorkflow(t, "k3d-test-gslb2", 5054)
		abstractTestFullFailover(t, ingressType.String(),
			workflowEU.Enrich(basePath, host, ingressType),
			workflowUS.Enrich(basePath, host, ingressType),
		)
	}
}

func abstractTestFullFailover(t *testing.T, testPrefix string, workflowEU, workflowUS *utils.Workflow) {
	workflowEU = workflowEU.WithTestApp("eu")
	instanceEU, err := workflowEU.Start()
	require.NoError(t, err)
	defer instanceEU.Kill()

	workflowUS = workflowUS.WithTestApp("us")
	instanceUS, err := workflowUS.Start()
	require.NoError(t, err)
	defer instanceUS.Kill()

	t.Run(fmt.Sprintf("%s failover on two concurrent clusters with podinfo running", testPrefix), func(t *testing.T) {
		err = instanceEU.WaitForAppIsRunning()
		require.NoError(t, err)
		err = instanceUS.WaitForAppIsRunning()
		require.NoError(t, err)
	})

	euLocalTargets := instanceEU.GetLocalTargets()
	usLocalTargets := instanceUS.GetLocalTargets()

	t.Run(fmt.Sprintf("%s kill podinfo on the second cluster", testPrefix), func(t *testing.T) {
		instanceUS.StopTestApp()
		err = instanceUS.WaitForExpected(euLocalTargets)
		require.NoError(t, err)
		err = instanceEU.WaitForExpected(euLocalTargets)
		require.NoError(t, err)
	})

	t.Run(fmt.Sprintf("%s kill podinfo on the first cluster", testPrefix), func(t *testing.T) {
		instanceEU.StopTestApp()
		err = instanceEU.WaitForExpected([]string{})
		require.NoError(t, err)
		err = instanceUS.WaitForExpected([]string{})
		require.NoError(t, err)
	})

	t.Run(fmt.Sprintf("%s start podinfo on the second cluster", testPrefix), func(t *testing.T) {
		instanceUS.StartTestApp()
		err = instanceUS.WaitForExpected(usLocalTargets)
		require.NoError(t, err)
		err = instanceEU.WaitForExpected(usLocalTargets)
		require.NoError(t, err)
	})

	t.Run(fmt.Sprintf("%s start podinfo on the first cluster", testPrefix), func(t *testing.T) {
		instanceEU.StartTestApp()
		err = instanceEU.WaitForExpected(euLocalTargets)
		require.NoError(t, err)
		err = instanceUS.WaitForExpected(euLocalTargets)
		require.NoError(t, err)
	})
}
